<?php
App::uses('AppModel', 'Model');
/**
 * Group Model
 *
 * @property User $User
 */
class Group extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'name' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'group_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	public $actsAs = array('Acl' => array('type' => 'requester'));
	
	public function parentNode() {
		return null;
    }
	
	public function afterSave($create, $options = array()){
	
		if($create){
			$id = $this->getLastInsertID();
			$aro = new Aro();
			$aro->updateAll(
				array('alias' => '\'Group:'.$id.'\''),
				array(
					'Aro.model' => 'Group',
					'Aro.foreign_key' => $id,
				)
			);
			
		} else {
		
			$aro = new Aro();
			$aro_record = $aro->findByForeignKey($this->id);
			
			if( !empty($aro_record) ){
				$this->Aro->save(array(
					'model' => $this->name,
					'foreign_key' => $this->id,
					'alias' => $this->name.":".$this->id,
					'id' => $aro_record['Aro']['id']
				));
			}
		}
		return true;
	}

}
